package jjn.carl.greedy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/8/1 13:34
 */
public class LeetCode406 {
    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, (first, second) -> {
            if (first[0] == second[0]) {
                return first[1] - second[1];
            }
            return second[0] - first[0];
        });
        List<int[]> list = new ArrayList<>();
        for (int[] p : people) {
            list.add(p[1], p);
        }
        return list.toArray(new int[people.length][]);
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        int[][] people = new int[count][2];
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < 2; j++) {
                people[i][j] = scanner.nextInt();
            }
        }
        int[][] reconstructQueue = new LeetCode406().reconstructQueue(people);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int[] element : reconstructQueue) {
            stringBuilder.append(Arrays.toString(element));
        }
        stringBuilder.append("]");
        System.out.println(stringBuilder);
    }
}
